﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using JIG_SoftTemplate.Common.Services;
using JIG_SoftTemplate.Resources.Language;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace JIG_SoftTemplate.ViewModels
{
    public partial class LoginPageViewModel : ViewModelBase
    {
        private readonly Common.Services.NavigationService navigationService;
        private readonly ISnackbarService snackbarService;
        private readonly LoggerService loggerService;

        public LoginPageViewModel(Common.Services.NavigationService navigationService,
            ISnackbarService snackbarService,
            LoggerService loggerService)
        {
            this.navigationService = navigationService;
            this.snackbarService = snackbarService;
            this.loggerService = loggerService;
        }

        [RelayCommand]
        void Setting()
        {
            navigationService.Navigate<SettingPageViewModel>();
        }


        #region 绑定变量

        [ObservableProperty]
        private string inputPwd;

        #endregion


        #region Button按钮事件
        /// <summary>
        /// Home按钮方法
        /// </summary>
        [RelayCommand]
        private void BtnHome()
        {
            try
            {
                loggerService.Info("退出登录窗口");
                //跳转到主页
                navigationService.Navigate<HomePageViewModel>();
            }
            catch (Exception ex)
            {
                loggerService.Error("退出登录窗口出错", ex);
                throw;
            }
        }

        /// <summary>
        /// Setting按钮方法
        /// </summary>
        [RelayCommand]
        private void BtnSetting()
        {
            try
            {
                loggerService.Info("按下登录按钮");
                if (InputPwd != null && InputPwd == "1234")
                {
                    loggerService.Info("输入密码正确");
                    //跳转到设置界面
                    navigationService.Navigate<SettingPageViewModel>();
                }
                else
                {
                    loggerService.Info("输入密码错误");
                    //错误提示
                    snackbarService.Show(
                        title: Lang.Snackbar_LoginPage_PwdWrongTitle,
                        message: Lang.Snackbar_LoginPage_PwdWrongMsg,
                        appearance: Wpf.Ui.Controls.ControlAppearance.Caution,
                        icon: new SymbolIcon(SymbolRegular.Info20),
                        timeout: TimeSpan.FromSeconds(3));
                }
            }
            catch (Exception ex)
            {
                loggerService.Error("退出登录窗口出错", ex);
                throw;
            }
        }
        #endregion
    }
}
